package com.cuckoo.cuckoo_app2.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cuckoo.cuckoo_app2.MyApplication;
import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.UserApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.UserApi;
import com.cuckoo.cuckoo_app2.dao.User;
import com.cuckoo.cuckoo_app2.utils.DialogUtils;
import com.google.gson.JsonObject;

public class SystemFeedbackAcitvity extends AppCompatActivity {

    private Context context;
    private ViewHolder holder;
    private UserApi userApi;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_feedback);

        this.context = this;
        this.holder = new ViewHolder();
        this.userApi = new UserApiImp();

        this.initView();

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initView() {
        holder.fc_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSubmit();
            }
        });
    }

    private void onSubmit() {
        User user = MyApplication.instances.getUser();
        if (user == null) {
            DialogUtils.showToast(context, "请先登录");
            return;
        }

        String feedback = holder.fc_inpurt.getText().toString();
        if (feedback.isEmpty()) {
            DialogUtils.showToast(context, "请输入反馈内容");
            return;
        }

        userApi.performSystemFeedbackAsync(feedback, user.getId(), new BaseCallbac<JsonObject>() {

            @Override
            public void onSuccess(JsonObject data) {
                if (!data.get("success").getAsBoolean()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DialogUtils.showToast(context, data.get("message").getAsString());
                        }
                    });
                    return;
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.showToast(context, "提交成功");
                    }
                });

                // 暂停一秒
                DialogUtils.sleep(1000);

                // 返回上一页
                finish();
            }

            @Override
            public void onFailure(String errorMessage) {

            }
        });

    }


    /**
     * 获取相应的ID
     */
    class ViewHolder {

        private EditText fc_inpurt;
        private Button fc_submit;

        public ViewHolder() {
            fc_inpurt = findViewById(R.id.fc_inpurt);
            fc_submit = findViewById(R.id.fc_submit);
        }
    }
}
